home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / URLStreamHandler.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  238 lines

  1. /*
  2.  * @(#)URLStreamHandler.java    1.23 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.util.Hashtable;
  22.  
  23. /**
  24.  * The abstract class <code>URLStreamHandler</code> is the common
  25.  * superclass for all stream protocol handlers. A stream protocol
  26.  * handler knows how to make a connection for a particular protocol
  27.  * type, such as <code>http</code>, <code>ftp</code>, or
  28.  * <code>gopher</code>.
  29.  *
  30.  * <p>In most cases, an instance of a <code>URLStreamHandler</code>
  31.  * subclass is not created directly by an application. Rather, the
  32.  * first time a protocol name is encountered when constructing a
  33.  * <code>URL</code>, the appropriate stream protocol handler is
  34.  * automatically loaded.
  35.  *
  36.  * @author  James Gosling
  37.  *
  38.  * @version 1.23, 07/01/98
  39.  *
  40.  * @see java.net.URL#URL(java.lang.String, java.lang.String, int,
  41.  * java.lang.String)
  42.  *
  43.  * @since JDK1.0 
  44.  */
  45. public abstract class URLStreamHandler {
  46.     /**
  47.      * Opens a connection to the object referenced by the 
  48.      * <code>URL</code> argument. 
  49.      * This method should be overridden by a subclass.
  50.      *
  51.      * @param      u   the URL that this connects to.
  52.      *
  53.      * @return a <code>URLConnection</code> object for the
  54.      * <code>URL</code>.
  55.      *
  56.      * @exception  IOException  if an I/O error occurs while opening the
  57.      *               connection.
  58.      * @since JDK1.0 
  59.      */
  60.     abstract protected URLConnection openConnection(URL u) throws IOException;
  61.  
  62.     /** 
  63.      * Parses the string representation of a <code>URL</code> into a 
  64.      * <code>URL</code> object. 
  65.      *
  66.      * <p>If there is any inherited context, then it has already been
  67.      * copied into the <code>URL</code> argument.
  68.      *
  69.      * <p> The <code>parseURL</code> method of
  70.      * <code>URLStreamHandler</code> parses the string representation
  71.      * as if it were an <code>http</code> specification. Most URL
  72.      * protocol families have a similar parsing. A stream protocol
  73.      * handler for a protocol that has a different syntax must
  74.      * override this routine.
  75.      *
  76.      * <p>If the file component of the URL argument contains a
  77.      * question mark (as with CGI HTTP URLs), the context is
  78.      * considered to be the URL's file component up to the first /
  79.      * before the question mark, not including the question mark or
  80.      * the directory before it. For example, if the URL was:
  81.      *
  82.      * <br><pre>    http://www.foo.com/dir/cgi-bin?foo=bar/baz</pre>
  83.      *
  84.      * and the spec argument was
  85.      *
  86.      * <br><pre>    quux.html</pre>
  87.      *
  88.      * the resulting URL would be:
  89.      *
  90.      * <br><pre>    http://www.foo.com/dir/quux.html</pre>.
  91.      * 
  92.      *
  93.      * @param u the <code>URL</code> to receive the result of parsing
  94.      * the spec.
  95.      *
  96.      * @param spec the <code>String</code> representing the URL that
  97.      * must be parsed.
  98.      *
  99.      * @param start the character index at which to begin
  100.      * parsing. This is just past the '<code>:</code>' (if there is
  101.      * one) that specifies the determination of the protocol name.
  102.      *
  103.      * @param limit the character position to stop parsing at. This is
  104.      * the end of the string or the position of the "<code>#</code>"
  105.      * character, if present. All information after the sharp sign
  106.      * indicates an anchor.
  107.      *
  108.      * @since JDK1.0 
  109.      */
  110.     protected void parseURL(URL u, String spec, int start, int limit) {
  111.     String protocol = u.getProtocol();
  112.     String host = u.getHost();
  113.     int port = u.getPort();
  114.     String file = u.getFile();
  115.     String ref = u.getRef();
  116.  
  117.     int i;
  118.     if ((start <= limit - 2) && (spec.charAt(start) == '/') &&
  119.         (spec.charAt(start + 1) == '/')) {
  120.         start += 2;
  121.         i = spec.indexOf('/', start);
  122.         if (i < 0) {
  123.         i = limit;
  124.         }
  125.         int prn = spec.indexOf(':', start);
  126.         port = -1;
  127.         if ((prn < i) && (prn >= 0)) {
  128.         try {
  129.             port = Integer.parseInt(spec.substring(prn + 1, i));
  130.         } catch(Exception e) {
  131.             // ignore bogus port numbers
  132.         }
  133.         if (prn > start) {
  134.             host = spec.substring(start, prn);
  135.         }
  136.         } else {
  137.         host = spec.substring(start, i);
  138.         }
  139.         start = i;
  140.         file = null;
  141.     } else if (host == null) {
  142.         host = "";
  143.     }
  144.     if (start < limit) {
  145.         /* 
  146.          * If the context URL is a CGI URL, the context to be the
  147.          * URL's file up to the / before ? character.
  148.          */
  149.         if (file != null) {
  150.         int questionMarkIndex = file.indexOf('?');
  151.         if (questionMarkIndex > -1) {
  152.             int lastSlashIndex = 
  153.             file.lastIndexOf('?', questionMarkIndex);
  154.             file = file.substring(0, ++lastSlashIndex);
  155.         }
  156.         }
  157.         if (spec.charAt(start) == '/') {
  158.         file = spec.substring(start, limit);
  159.         } else if (file != null && file.length() > 0) {
  160.         /* relative to the context file - use either 
  161.          * Unix separators || platform separators */
  162.         int ind = Math.max(file.lastIndexOf('/'), 
  163.                    file.lastIndexOf(File.separatorChar));
  164.  
  165.         file = file.substring(0, ind) + "/" + spec.substring(start, 
  166.                                      limit);
  167.         } else {
  168.         file = "/" + spec.substring(start, limit);
  169.         }
  170.     }
  171.     if ((file == null) || (file.length() == 0)) {
  172.         file = "/"; 
  173.     }
  174.     while ((i = file.indexOf("/./")) >= 0) {
  175.         file = file.substring(0, i) + file.substring(i + 2);
  176.     }
  177.     while ((i = file.indexOf("/../")) >= 0) {
  178.         if ((limit = file.lastIndexOf('/', i - 1)) >= 0) {
  179.         file = file.substring(0, limit) + file.substring(i + 3);
  180.         } else {
  181.         file = file.substring(i + 3);
  182.         }
  183.     }
  184.  
  185.     setURL(u, protocol, host, port, file, ref);
  186.     }
  187.  
  188.     /**
  189.      * Converts a <code>URL</code> of a specific protocol to a 
  190.      * <code>String</code>. 
  191.      *
  192.      * @param   u   the URL.
  193.      * @return  a string representation of the <code>URL</code> argument.
  194.      * @since   JDK1.0
  195.      */
  196.     protected String toExternalForm(URL u) {
  197.     String result = u.getProtocol() + ":";
  198.     if ((u.getHost() != null) && (u.getHost().length() > 0)) {
  199.         result = result + "//" + u.getHost();
  200.         if (u.getPort() != -1) {
  201.         result += ":" + u.getPort();
  202.         }
  203.     }
  204.     result += u.getFile();
  205.     if (u.getRef() != null) {
  206.         result += "#" + u.getRef();
  207.     }
  208.     return result;
  209.     }
  210.  
  211.     /**
  212.      * Sets the fields of the <code>URL</code> argument to the
  213.      * indicated values.  Only classes derived from URLStreamHandler
  214.      * are supposed to be able to call the set method on a URL.
  215.      *
  216.      * @param   u         the URL to modify.
  217.      * @param   protocol  the protocol name.
  218.      * @param   host      the remote host value for the URL.
  219.      * @param   port      the port on the remote machine.
  220.      * @param   file      the file.
  221.      * @param   ref       the reference.
  222.      *
  223.      * @see java.net.URL#set(java.lang.String, java.lang.String, int,
  224.      * java.lang.String, java.lang.String)
  225.      *
  226.      * @since JDK1.0 
  227.      */
  228.     protected void setURL(URL u, String protocol, String host, int port,
  229.               String file, String ref) {
  230.     if (this != u.handler) {
  231.         throw new SecurityException("handler for url different from " +
  232.                     "this handler");
  233.     }
  234.     // ensure that no one can reset the protocol on a given URL.
  235.         u.set(u.getProtocol(), host, port, file, ref);
  236.     }
  237. }
  238.